package com.gua.jbs.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.gua.jbs.dao.IProductDao;
import com.gua.jbs.pojo.Jbspit;
import com.gua.jbs.utility.ApplicationException;
import com.gua.jbs.utility.DBConstant;
import com.gua.jbs.utility.DateUtil;
import com.gua.jbs.utility.ExceptionMessage;
import com.gua.jbs.utility.QueryConstant;
import com.gua.jbs.utility.StringUtil;

public class ProductDao extends BaseDao implements IProductDao {

	public String saveProduct(Jbspit jbspit) throws ApplicationException {
		// Prepare the parameters
		List lParamList = new ArrayList();
		boolean isSaved = false;
		int pk = 0;
		if (jbspit != null) {
			// set the parameters

			try {
				pk = getPrimaryKey("PROD_ID", "JBSPIT");
				lParamList.add(0, pk);
				lParamList.add(1, jbspit.getProdName());
				lParamList.add(2, jbspit.getProdColor());
				lParamList.add(3,
						jbspit.getProdSize() == ""
								|| jbspit.getProdSize() == null ? "0.0"
								: jbspit.getProdSize());
				lParamList.add(4, jbspit.getProdweight());
				lParamList.add(5, jbspit.getProdCode());
				lParamList.add(6, jbspit.getProdType());
				String image = jbspit.getProdImage();
				if (StringUtil.isNullOrEmpty(image)) {
					String editImage = image.replace("\\", "@");
					lParamList.add(7, editImage);
				} else
					lParamList.add(7, null);

				// lParamList.add(7, image);
				lParamList.add(8, jbspit.getIsApproved());
				lParamList.add(9, DBConstant.IS_ACTIVE_YES);
				lParamList.add(10,
						new java.sql.Date(System.currentTimeMillis()));
				lParamList.add(11,
						new java.sql.Date(System.currentTimeMillis()));
				lParamList.add(12, jbspit.getExtrafield1());
				lParamList.add(13, jbspit.getExtrafield2());
				lParamList.add(14, jbspit.getExtrafield3());
				lParamList.add(15, jbspit.getCreatedBy());
				lParamList.add(16, jbspit.getModifiedBy());
				lParamList.add(17,
						jbspit.getProdPrice() == ""
								|| jbspit.getProdPrice() == null ? "0.0"
								: jbspit.getProdPrice());
				lParamList.add(18, jbspit.getDesignCode());
				lParamList.add(19, jbspit.getHrCode());
				lParamList.add(20, jbspit.getProdSubType());
				lParamList.add(21, jbspit.getProdCost());

				// Get the query by passing parameters
				String query = getQuery(QueryConstant.SAVE_PRODUCT, lParamList);
				int resultCount = insert(query);
				if (resultCount != 0) {
					isSaved = true;
				} else
					isSaved = false;
				closeConnection();
			} catch (SQLException sqe) {
				throw new ApplicationException(
						ExceptionMessage.PRODUCT_NOT_SAVED);
			}
		}
		if (isSaved)
			return pk + "";
		else
			return null;
	}

	public boolean updateProduct(Jbspit jbspit) throws ApplicationException {
		// Prepare the parameters
		List lParamList = new ArrayList();
		boolean isUpdated = false;

		if (jbspit != null) {
			// set the parameters
			lParamList.add(0, jbspit.getProdId());
			lParamList.add(1, jbspit.getProdName());
			lParamList.add(2, jbspit.getProdColor());
			lParamList
					.add(3,
							jbspit.getProdSize() == ""
									|| jbspit.getProdSize() == null ? "0.0"
									: jbspit.getProdSize());
			lParamList.add(4, jbspit.getProdweight());
			//lParamList.add(5, jbspit.getProdCode());
			lParamList.add(5, jbspit.getProdType());
			String image = jbspit.getProdImage();
			if (StringUtil.isNullOrEmpty(image)) {
				String editImage = image.replace("\\", "@");
				lParamList.add(6, editImage);
			} else
				lParamList.add(6, null);

			// lParamList.add(7, image);
			lParamList.add(7, jbspit.getIsApproved());
			lParamList.add(8, DBConstant.IS_ACTIVE_YES);
			// lParamList.add(10, new
			// java.sql.Date(System.currentTimeMillis()));
			lParamList.add(9, new java.sql.Date(System.currentTimeMillis()));
			lParamList.add(10, jbspit.getExtrafield1());
			lParamList.add(11, jbspit.getExtrafield2());
			lParamList.add(12, jbspit.getExtrafield3());
			lParamList.add(13, jbspit.getCreatedBy());
			lParamList.add(14, jbspit.getModifiedBy());
			lParamList.add(
					15,
					jbspit.getProdPrice() == ""
							|| jbspit.getProdPrice() == null ? "0.0" : jbspit
							.getProdPrice());
			lParamList.add(16, jbspit.getDesignCode());
			lParamList.add(17, jbspit.getHrCode());
			lParamList.add(18, jbspit.getProdSubType());
			lParamList.add(19, jbspit.getProdCost());

			// Get the query by passing parameters
			String query = getQuery(QueryConstant.UPDATE_PRODUCT_ON_ID,
					lParamList);
			int resultCount = 0;
			try {
				resultCount = update(query);
				if (resultCount != 0) {
					isUpdated = true;
				} else
					isUpdated = false;
				closeConnection();
			} catch (SQLException sqe) {
				throw new ApplicationException(
						ExceptionMessage.PRODUCT_NOT_UPDATED);
			}
		}
		return isUpdated;
	}

	@Override
	public boolean deleteProductDao(String prodId) throws ApplicationException {
		boolean isDeleted = false;

		List<String> lParamList = new ArrayList<String>();
		lParamList.add(prodId);
		String query = getQuery(QueryConstant.deleteProduct, lParamList);
		int resultCount = 0;
		try {
			resultCount = delete(query);
			if (resultCount != 0) {
				isDeleted = true;
			} else
				isDeleted = false;
		} catch (SQLException sqe) {
			throw new ApplicationException(ExceptionMessage.PRODUCT_NOT_DELETED);
		}
		return isDeleted;
	}

	public Jbspit findProductByCode(String prodCode)
			throws ApplicationException {
		Jbspit objPit = new Jbspit();
		List lParamList = new ArrayList();
		lParamList.add(0, prodCode);
		lParamList.add(1, DBConstant.IS_ACTIVE_YES);
		// Get the query by passing parameters
		String query = getQuery(QueryConstant.GET_PRODUCT_BY_CODE, lParamList);
		ResultSet rs = null;
		try {
			rs = getResults(query);
			if (rs != null) {
				while (rs.next()) {
					objPit.setProdId(String.valueOf(rs.getInt("PROD_ID")));
					objPit.setProdName(rs.getString("PROD_NAME"));
					objPit.setProdPrice(rs.getString("PROD_PRICE"));
					objPit.setExtrafield1(rs.getString("EXTRA_FIELD1"));
				}
			}
			closeConnection();
		} catch (SQLException sqe) {
			throw new ApplicationException(
					ExceptionMessage.PRODUCT_NOT_IN_SYSTEM);
		}
		return objPit;
	}

	public Jbspit findProductByPk(String prodId) throws ApplicationException {
		Jbspit objPit = new Jbspit();
		List lParamList = new ArrayList();
		lParamList.add(0, prodId);
		lParamList.add(1, DBConstant.IS_ACTIVE_YES);
		// Get the query by passing parameters
		String query = getQuery(QueryConstant.GET_PRODUCT_BY_ID, lParamList);
		ResultSet rs = null;
		try {
			rs = getResults(query);
			if (rs != null) {
				while (rs.next()) {
					objPit.setExtrafield3(rs.getString("STOCK"));
				}
			}
		} catch (SQLException sqe) {
			throw new ApplicationException(ExceptionMessage.STOCK_NOT_RETRIEVED);
		}
		return objPit;
	}

	@Override
	public List<Jbspit> filterProductsByDate(String fromDate, String toDate)
			throws ApplicationException {
		// TODO Auto-generated method stub
		List<Jbspit> list_products = new ArrayList<Jbspit>();
		List lParamList = new ArrayList();

		// set the parameters
		lParamList.add(0, fromDate);
		lParamList.add(1, toDate);

		String query = getQuery(QueryConstant.GET_PRODUCTS_BY_DATE, lParamList);
		try {
			ResultSet rs = getResults(query);
			while (rs.next()) {

				Jbspit objPit = new Jbspit();
				objPit.setProdId(String.valueOf(rs.getInt("PROD_ID")));
				objPit.setProdName(rs.getString("PROD_NAME"));
				objPit.setProdColor(rs.getString("PROD_COLOR"));
				objPit.setProdSize(String.valueOf(rs.getBigDecimal("PROD_SIZE")));
				objPit.setProdweight(String.valueOf(rs
						.getBigDecimal("PROD_WEIGHT")));
				objPit.setProdCode(rs.getString("PROD_CODE"));
				objPit.setProdType(rs.getString("PROD_TYPE"));
				objPit.setProdImage(String.valueOf(rs
						.getBinaryStream("PROD_IMAGE")));
				// objPit.setApproved(true);
				objPit.setActive(true);
				objPit.setCreateDate(rs.getDate("CREATED_DT"));
				objPit.setModifyDate(rs.getDate("MODIFY_DT"));
				objPit.setExtrafield1(rs.getString("EXTRA_FIELD1"));
				objPit.setExtrafield2(rs.getString("EXTRA_FIELD2"));
				objPit.setExtrafield3(rs.getString("EXTRA_FIELD3"));
				objPit.setCreatedBy(rs.getString("CREATED_BY"));
				objPit.setCreatedBy(rs.getString("MODIFIED_BY"));
				list_products.add(objPit);

			}
			return list_products;

		} catch (SQLException sqe) {
			return null;
		}

	}

	@Override
	public Map<Integer, Map<String, String>> productsType()
			throws ApplicationException {

		Map<Integer, Map<String, String>> map_prodType = new LinkedHashMap<Integer, Map<String, String>>();
		List lParamList = new ArrayList();
		lParamList.add(0, DBConstant.IS_PARENT_YES);

		String query = getQuery(QueryConstant.GET_PRODUCT_TYPES, lParamList);
		try {
			ResultSet rs = getResults(query);
			while (rs.next()) {
				String prodCatName = rs.getString("CATEGORY_NAME");
				String prodCatCode = rs.getString("EXTRA_FIELD1");
				Map<String, String> categoryMap = new LinkedHashMap<String, String>();
				categoryMap.put(prodCatName, prodCatCode);
				map_prodType.put(rs.getInt("CATEGORY_MASTER_ID"), categoryMap);

				/*
				 * boolean isParent=false; Jbscmt objCmt = new Jbscmt();
				 * objCmt.setCatMasterId
				 * (String.valueOf(rs.getInt("CATEGORY_MASTER_ID")));
				 * objCmt.setCatName(rs.getString("CATEGORY_NAME"));
				 * objCmt.setParentId((rs.getString("PARENT_ID")));
				 * if(DBConstant
				 * .IS_PARENT_YES.equals(rs.getString("PARENT_ID"))){
				 * isParent=true; } objCmt.setParent(isParent);
				 * objCmt.setActive(true);
				 * objCmt.setCreateDate(rs.getDate("CREATED_DT"));
				 * objCmt.setModifyDate(rs.getDate("MODIFY_DT"));
				 * objCmt.setExtrafield1(rs.getString("EXTRA_FIELD1"));
				 * objCmt.setExtrafield2(rs.getString("EXTRA_FIELD2"));
				 * objCmt.setExtrafield3(rs.getString("EXTRA_FIELD3"));
				 * objCmt.setCreatedBy(rs.getString("CREATED_BY"));
				 * objCmt.setCreatedBy(rs.getString("MODIFIED_BY"));
				 * list_products.add(objCmt);
				 */
			}
			return map_prodType;
		} catch (SQLException sqe) {
			return null;
		}
	}

	@Override
	public Map<Integer, Map<String, String>> productsSubType()
			throws ApplicationException {
		// TODO Auto-generated method stub
		Map<Integer, Map<String, String>> map_prodSubType = new LinkedHashMap<Integer, Map<String, String>>();
		List lParamList = new ArrayList();
		lParamList.add(0, DBConstant.IS_PARENT_NO);

		String query = getQuery(QueryConstant.GET_PRODUCT_TYPES, lParamList);
		try {
			ResultSet rs = getResults(query);
			while (rs.next()) {
				String prodCatName = rs.getString("CATEGORY_NAME");
				String prodCatCode = rs.getString("EXTRA_FIELD1");
				Map<String, String> categoryMap = new LinkedHashMap<String, String>();
				categoryMap.put(prodCatName, prodCatCode);
				map_prodSubType.put(rs.getInt("CATEGORY_MASTER_ID"),
						categoryMap);
			}
			return map_prodSubType;
		} catch (SQLException sqe) {
			return null;
		}

	}

	@Override
	public Map<Integer, Map<String, String>> productsPolish()
			throws ApplicationException {
		// TODO Auto-generated method stub
		Map<Integer, Map<String, String>> map_prodPolish = new LinkedHashMap<Integer, Map<String, String>>();
		List lParamList = new ArrayList();
		lParamList.add(0, DBConstant.CODE_GROUP_POLISH);

		String query = getQuery(QueryConstant.GET_PRODUCT_POLISH, lParamList);
		try {
			ResultSet rs = getResults(query);
			while (rs.next()) {
				String prodCatName = rs.getString("CODE_NAME");
				String prodCatCode = rs.getString("CODE_VALUE");
				Map<String, String> categoryMap = new LinkedHashMap<String, String>();
				categoryMap.put(prodCatName, prodCatCode);
				map_prodPolish.put(rs.getInt("CODE_ID"), categoryMap);
			}
			return map_prodPolish;
		} catch (SQLException sqe) {
			return null;
		}
	}

	@Override
	public Map<Integer, Map<String, String>> productsColor()
			throws ApplicationException {
		// TODO Auto-generated method stub
		Map<Integer, Map<String, String>> map_prodColor = new LinkedHashMap<Integer, Map<String, String>>();
		List lParamList = new ArrayList();
		lParamList.add(0, DBConstant.CODE_GROUP_COLOR);

		String query = getQuery(QueryConstant.GET_PRODUCT_COLOR, lParamList);
		try {
			ResultSet rs = getResults(query);
			while (rs.next()) {
				String prodCatName = rs.getString("CODE_NAME");
				String prodCatCode = rs.getString("CODE_VALUE");
				Map<String, String> categoryMap = new LinkedHashMap<String, String>();
				categoryMap.put(prodCatName, prodCatCode);
				map_prodColor.put(rs.getInt("CODE_ID"), categoryMap);
			}
			return map_prodColor;
		} catch (SQLException sqe) {
			return null;
		}
	}

	@Override
	public Map<Integer, Map<String, String>> productsSize()
			throws ApplicationException {
		// TODO Auto-generated method stub
		Map<Integer, Map<String, String>> map_prodSize = new LinkedHashMap<Integer, Map<String, String>>();
		List lParamList = new ArrayList();
		lParamList.add(0, DBConstant.CODE_GROUP_SIZE);

		String query = getQuery(QueryConstant.GET_PRODUCT_SIZE, lParamList);
		try {
			ResultSet rs = getResults(query);
			while (rs.next()) {
				String prodCatName = rs.getString("CODE_NAME");
				String prodCatCode = rs.getString("CODE_VALUE");
				Map<String, String> categoryMap = new LinkedHashMap<String, String>();
				categoryMap.put(prodCatName, prodCatCode);
				map_prodSize.put(rs.getInt("CODE_ID"), categoryMap);
			}
			return map_prodSize;
		} catch (SQLException sqe) {
			return null;
		}
	}

	/**
	 * Method to get the barCode counter
	 * 
	 * @return
	 * @throws ApplicationException
	 */
	public String getBarCodeCounter() throws ApplicationException {
		List lParamList = new ArrayList();
		lParamList.add(0, DBConstant.CODE_NAME_BARCODE);
		String query = getQuery(QueryConstant.GET_BAR_CODE_COUNT_VALUE,
				lParamList);
		String prodCatCode = "";
		try {
			ResultSet rs = getResults(query);
			while (rs.next()) {
				prodCatCode = rs.getString("CODE_VALUE");
			}
		} catch (SQLException sqe) {
			throw new ApplicationException(ExceptionMessage.INVALID_GENERATOR);
		}
		return prodCatCode;
	}

	/**
	 * Method to update the barCode value
	 * 
	 * @param generatorCounter
	 * @return
	 */
	public boolean updateGeneratorVal(String generatorCounter)
			throws ApplicationException {
		boolean isUpdated = false;
		List lParamList = new ArrayList();
		if (StringUtil.isNullOrEmpty(generatorCounter)) {
			lParamList.add(0, "" + ((Integer.parseInt(generatorCounter)) + 1));
			lParamList.add(1, DBConstant.CODE_NAME_BARCODE);
			String query = getQuery(QueryConstant.UPDATE_BAR_CODE_COUNT_VALUE,
					lParamList);
			try {
				int rowCount = update(query);
				if (rowCount != 0)
					isUpdated = true;
			} catch (SQLException sqe) {
				throw new ApplicationException(
						ExceptionMessage.INVALID_GENERATOR);
			}
		}
		return isUpdated;
	}
}
